package com.ziyia.a8_1_2;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;

import java.util.HashSet;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private SharedPreferences preferences;
    private SharedPreferences.Editor editor;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        preferences = getSharedPreferences("8-1-2", MODE_PRIVATE);
        editor = preferences.edit();
    }

    public void read(View view) {

        Map<String, ?> all = preferences.getAll();

        new AlertDialog.Builder(this)
                .setTitle(getLocalClassName())
                .setMessage(all.toString())
                .create()
                .show();
    }

    public void write(View view) {

        HashSet<String> set = new HashSet<>();
        set.add("abc");
        set.add("efb");
        editor.putString("username", "root");
        editor.putString("password", "123456");
        editor.putInt("age", 18);
        editor.putString("email", "3051142026@qq.com");
        editor.putString("email", "123456@qq.com");
        editor.putStringSet("admin", set);

        // 当当前线程提交[不推荐]
//        editor.commit();

        // 在后台提交[推荐]
        editor.apply();
    }
}